//////////////////////////////////////////////////////////////////////////
//
// This file is a part of APNS provider project.
// Copyright (c) Vladimir Gumenuk, 2013-2014.
//
// PROJECT:         APNS provider
// FILE:            apns_test.cpp : test console application.
// AUTHOR:          Vladimir Gumenuk
// DATE:            22-Dec-2013
// MODIFICATIONS:
//
//////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "APNSClient.h"

CAPNSModule _AtlModule;

// PROD: gateway.push.apple.com, port 2195, feedback.push.apple.com
// DEV:  gateway.sandbox.push.apple.com, port 2195, feedback.sandbox.push.apple.com

// test devices and tokens
static DeviceInfo g_RegDevices[] = {
    { "Vladimir Gumenuk", "com.bwin.sportsbook.at.dev",    0xcd,0x2e,0x73,0x9a,0x24,0x37,0x21,0xdb,0x9b,0x50,0xb2,0xad,0x42,0x59,0x6e,0x53,0x51,0x3e,0x23,0x6f,0xaf,0xd0,0xc4,0x8e,0x9c,0x61,0xfc,0x16,0x62,0xbc,0x7b,0xce },
    { "Olexander Cheboraka", "com.bwin.sportsbook.at.dev", 0xb0,0xe0,0xbb,0x39,0xb2,0xf0,0x03,0xc2,0x2e,0xd7,0x96,0x49,0x42,0xe1,0x4f,0x72,0xca,0xb9,0x0f,0xe0,0x0a,0x54,0xe3,0x82,0xc3,0x20,0x9b,0xbf,0x16,0xf3,0x7c,0x80 },
    { "Pavel Kostenko", "com.bwin.sportsbook.at.dev",      0xf8,0x77,0x41,0x68,0x1a,0x65,0x91,0x1a,0x88,0xe7,0x25,0x25,0xef,0x25,0x0b,0x37,0xe4,0xc6,0x0c,0xda,0xb6,0x2e,0x37,0x6c,0x05,0xc2,0x11,0xa5,0x40,0xc7,0xc7,0xc1 },
    { "Konstanin Kleba", "com.bwin.sportsbook.at.dev",     0x1d,0xb8,0x8e,0xc8,0x51,0x48,0xd4,0xc2,0xa4,0x40,0xe1,0x3d,0x93,0x36,0x4d,0xd7,0x3d,0xbb,0x76,0xdd,0x18,0xd5,0xf3,0xaa,0xdd,0xcc,0x0b,0xe6,0x8d,0x6e,0xbd,0x18 },
    { "Roman Oliichuk", "com.bwin.sportsbook.at.dev",      0xec,0x1e,0x02,0x68,0x56,0x2e,0xdd,0xcc,0x78,0x38,0xd5,0xbd,0xf8,0x1e,0x7d,0x59,0x13,0xbd,0xd5,0xb0,0x8d,0xd9,0x70,0xb1,0x93,0xf5,0x7a,0x8d,0x61,0x89,0xfd,0xa6 }
};

#define NUM_CLIENTS (sizeof(g_RegDevices)/sizeof(DeviceInfo))

static LPCTSTR g_lpszAppBundleID = _T("com.bwin.sportsbook.at.dev");
static LPCTSTR g_lpszAPNSrv = _T("gateway.sandbox.push.apple.com");
static DWORD   g_dwAPNSPort = 2195;

HRESULT CreatePushNotificationsObjects( IPushNotification *pTargets[NUM_CLIENTS] )
{
    HRESULT hr = E_FAIL;

    for( int i = 0; i < NUM_CLIENTS; i++ )
    {
        CComPtr<IPushNotification> spNtfs;
        if( SUCCEEDED( hr = CreatePushNotification( &spNtfs ) ) )
        {
            char szBuf[MAX_NTFS_LEN];
            sprintf( szBuf, "{ \"aps\":{\"alert\":\"Hello %s!\"}}", g_RegDevices[i].szUserName );
            if( SUCCEEDED( hr = spNtfs->Init( 0, 10, g_RegDevices[i].token, szBuf ) ) )
            {
                pTargets[i] = spNtfs.Detach();
            } else
                break;
        } else
            break;
    }

    return hr;
}

void DeletePushNotificationsObjects( IPushNotification *pTargets[NUM_CLIENTS] )
{
    for( int i = 0; i < NUM_CLIENTS; i++ )
    {
        pTargets[i]->Release();
        pTargets[i] = NULL;
    }
}

int _tmain( int argc, _TCHAR* argv[] )
{
    HRESULT hr = CoInitializeEx( NULL, 0 );
    if( hr )
        return -1;

    LogOpen( NULL );
    LogSetLevel( 3 );
#ifdef _DEBUG
    LogEnableDbgTrace( TRUE );
#endif

    IGMSNetwork *pNetwork = NULL;
    if( SUCCEEDED( hr = CreateGMSNetwork( &pNetwork ) ) )
    {
        if( SUCCEEDED( hr = pNetwork->InitNetLayer() ) ) 
        {
            CComPtr<IAPNSClient> spClient;
            if( SUCCEEDED( hr = CreateAPNSClient( &spClient ) ) )
            {
                if( SUCCEEDED( hr = spClient->Connect( pNetwork, g_lpszAppBundleID, g_lpszAPNSrv, g_dwAPNSPort ) ) )
                {
                    LOG3( _T("Connected to: %s, press ANY key to stop\n"), g_lpszAPNSrv );

                    IPushNotification *pTargets[NUM_CLIENTS] = { NULL }; 
                    if( SUCCEEDED( hr = CreatePushNotificationsObjects( pTargets ) ) )
                    {
                        DWORD dwNumNotifications = 0;
                        while( TRUE )
                        {
                            Sleep( 1000 );

                            LOG1( _T("Sending PN #%ld\n"), dwNumNotifications );
                            char szBuf[MAX_NTFS_LEN];
                        
                            for( int i = 0; i < NUM_CLIENTS; i++ )
                            {
                                sprintf( szBuf, "{ \"aps\":{\"alert\":\"Hello %s! No: %ld\"}}", g_RegDevices[i].szUserName, dwNumNotifications );
                                pTargets[i]->SetPayload( szBuf );

                                if( FAILED( hr = spClient->SendNotification( pTargets[i] ) ) )
                                    break;
                            }

                            //if( FAILED( hr = client.SendNotifications( pTargets ) ) )
                            //     break;

                            dwNumNotifications++;

                            if( _kbhit() )
                                break;
                        }

                        LOG1( _T("Total notifications send: %ld\n"), dwNumNotifications * NUM_CLIENTS );
                        DeletePushNotificationsObjects( pTargets );
                    }

                    spClient->Disconnect();
                    LOG3( _T("Session closed\n") );
                }
            }

            pNetwork->ShutNetLayer();
        }

        SAFE_RELEASE( pNetwork );
    }

    LogClose();

    CoUninitialize();
	return 0;
}

